/*
 * @Author: wangwenshuai 644586996@qq.com
 * @Date: 2022-11-07 15:33:51
 * @LastEditors: wangwenshuai 644586996@qq.com
 * @LastEditTime: 2023-02-27 14:01:14
 * @FilePath: \CarbonPccountingProject\carbon_accounting_project\src\api\manage.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Vue from "vue";
import { axios } from "@/utils/request";
import signMd5Utils from "@/utils/encryption/signMd5Utils";
/**
 * api常用地址对象
 */
const api = {};
export default api;

/**
 *
 * @param {*} url 地址
 * @param {*} parameter 参数
 * @returns
 */
//post
export function postAction(url, parameter) {
  let sign = signMd5Utils.getSign(url, parameter);
  let signHeader = {
    "X-Sign": sign,
    "X-TIMESTAMP": signMd5Utils.getTimestamp(),
  };
  return axios({
    url: url,
    method: "post",
    data: parameter,
    headers: signHeader,
  });
}

//post method= {post | put}
export function httpAction(url, parameter, method) {
  let sign = signMd5Utils.getSign(url, parameter);
  let signHeader = {
    "X-Sign": sign,
    "X-TIMESTAMP": signMd5Utils.getTimestamp(),
  };
  return axios({
    url: url,
    method: method,
    data: parameter,
    headers: signHeader,
  });
}

//put
export function putAction(url, parameter) {
  let sign = signMd5Utils.getSign(url, parameter);
  let signHeader = {
    "X-Sign": sign,
    "X-TIMESTAMP": signMd5Utils.getTimestamp(),
  };
  return axios({
    url: url,
    method: "put",
    data: parameter,
    headers: signHeader,
  });
}

//get
export function getAction(url, parameter) {
  let sign = signMd5Utils.getSign(url, parameter);
  let signHeader = {
    "X-Sign": sign,
    "X-TIMESTAMP": signMd5Utils.getTimestamp(),
  };
  //   console.log(sign)
  //   console.log(url, parameter)
  return axios({
    url: url,
    method: "get",
    params: parameter,
    headers: signHeader,
  });
}

//deleteAction
export function deleteAction(url, parameter) {
  return axios({
    url: url,
    method: "delete",
    params: parameter,
  });
}

/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile(url, parameter, method = "get") {
  if (method == "get") {
    return axios({
      url: url,
      params: parameter,
      method: method,
      responseType: "blob",
    });
  } else {
    return axios({
      url: url,
      method: method,
      data: parameter,
      responseType: "blob",
    });
  }
}

/**
 * 获取文件服务访问路径
 * @param avatar
 * @param subStr
 * @returns {*}
 */
export function getFileAccessHttpUrl(avatar, subStr) {
  if (!subStr) subStr = "http";
  try {
    if (avatar && avatar.startsWith(subStr)) {
      return avatar;
    } else {
      if (avatar && avatar.length > 0 && avatar.indexOf("[") == -1) {
        return window._CONFIG["staticDomainURL"] + "/" + avatar;
      }
    }
  } catch (err) {
    return;
  }
}
